package com.beteng.su.betengsu.net.webservice;

import android.content.Context;
import android.support.annotation.NonNull;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
import com.beteng.su.betengsu.bean.AreaBean;
import com.beteng.su.betengsu.bean.BaseBean;
import com.beteng.su.betengsu.bean.OrderBean;
import com.beteng.su.betengsu.bean.OrderTranceBean;
import com.beteng.su.betengsu.bean.UpdataBean;
import com.beteng.su.betengsu.bean.UserBean;
import com.beteng.su.betengsu.net.Api;
import com.beteng.su.betengsu.utils.CommonUtils;
import com.blankj.utilcode.util.NetworkUtils;


import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.schedulers.Schedulers;


public class SiteService {
    private static final String TAG = "SiteService";
    private static SiteService siteService;

    private Context mContext;

    public static SiteService getInstants() {
        if (siteService == null) {
            siteService = new SiteService();
        }
        return siteService;
    }


    //获取是否资产盘点状态
    public Observable<UserBean> getLogin(HashMap<String, Object> params, Context context) {
        return (Observable<UserBean>) callWebService(UserBean.class, new WebService(Api.APP_DOMAIN
                ,params, CommonUtils.Method.LOGIN,context));
    }

    //获取单号信息
    public Observable<List<OrderBean>> getOrders(HashMap<String, Object> params, Context context) {
        return (Observable<List<OrderBean>>) callWebService(OrderBean.class, new WebService(Api.APP_DOMAIN
                ,params, CommonUtils.Method.GetOrderList,context));
    }

    //提交签收信息
    public Observable<BaseBean> commitSignOrder(HashMap<String, Object> params, Context context) {
        return (Observable<BaseBean>) callWebService(BaseBean.class, new WebService(Api.APP_DOMAIN
                ,params, CommonUtils.Method.Sign,context));
    }

    //提交异常信息
    public Observable<BaseBean> commitErrorOrder(HashMap<String, Object> params, Context context) {
        return (Observable<BaseBean>) callWebService(BaseBean.class, new WebService(Api.APP_DOMAIN
                ,params, CommonUtils.Method.Defect,context));
    }

    //提交录入信息
    public Observable<BaseBean> commitOrderInfo(HashMap<String, Object> params, Context context) {
        return (Observable<BaseBean>) callWebService(BaseBean.class, new WebService(Api.APP_DOMAIN
                ,params, CommonUtils.Method.WaybillTrace,context));
    }

    //查询跟踪信息
    public Observable<List<OrderTranceBean>> getWaybillTrace(HashMap<String, Object> params, Context context) {
        return (Observable<List<OrderTranceBean>>) callWebService(OrderTranceBean.class, new WebService(Api.APP_DOMAIN
                ,params, CommonUtils.Method.GetWaybillTrace,context));
    }

    //获取Area地区
    public Observable<String> getArea(HashMap<String, Object> params, Context context) {
        return (Observable<String>) callWebService(String.class, new WebService(Api.APP_DOMAIN
                ,params, CommonUtils.Method.UpdateArea,context));
    }

    //获取版本信息
    public Observable<UpdataBean> getVersion(Context context) {
        return (Observable<UpdataBean>) callWebService(UpdataBean.class, new WebService(Api.APP_DOMAIN
                , CommonUtils.Method.Version,context));
    }


    public Observable<?> callWebService(final Class subclass, final WebService webService) {
        return Observable.create(new ObservableOnSubscribe<Object>() {
            @Override
            public void subscribe(@NonNull ObservableEmitter<Object> subscriber) throws Exception {
                //订阅者回调 onNext 和 onCompleted

                if (!NetworkUtils.isAvailableByPing("baidu.com")) {
                    subscriber.onError(new Throwable("网络不可用"));
                    return;
                }
                WebServiceResultEntity result = webService.call();
                if (result.getSuccess()) {
                    if (subclass == String.class) {
                        subscriber.onNext(result.getData());
                        return;
                    }
                    if (result.getStatus()==200){
                        try{
                            subscriber.onNext(jsonParser(subclass, (String) result.getData()));
                        }catch (JSONException e){
                            try {
                                subscriber.onNext(getListFromJSON((String) result.getData(),subclass));
                            } catch (JSONException e1) {
                                try {
                                    subscriber.onNext(jsonParser(subclass, result.getResult()));
                                } catch (JSONException e2) {
                                    e2.printStackTrace();
                                }

                            }

                        }

                    }else {
                        subscriber.onError(new Throwable(result.getMessage()));
                    }

                } else {
                    subscriber.onError(new Throwable("请求超时"));
                }

                subscriber.onComplete();
            }


        }).subscribeOn(Schedulers.io());
    }

    public Object jsonParser(Type mType, String jsonStr) {
        return JSON.parseObject(jsonStr,mType);
    }

    public static  List getListFromJSON(String json, Class type) {
        return JSON.parseArray(json,type);
    }



}
